package com.xiaomi.dao.impl;

import com.xiaomi.dao.UserDao;
import com.xiaomi.entity.User;
import com.xiaomi.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

/**
 * TODO
 *
 * @ClassName UserDaoImpl
 * @Author jy
 * @Date 2021/5/22
 */
public class UserDaoImpl implements UserDao {

    private final QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());

    @Override
    public User checkUser(String username) throws Exception {
        String sql = "select * from user where username=?";
        return queryRunner.query(sql, new BeanHandler<>(User.class), username);
    }

    @Override
    public int register(User user) throws Exception {
        Object[] objects = {user.getUsername(), user.getPassword(), user.getEmail(), user.getGender(),
                user.getState(), user.getRole(), user.getCode()};
        String sql = "insert into user values(default,?,?,?,?,?,?,?)";
        return queryRunner.update(sql, objects);
    }

    @Override
    public User login(String username, String password) throws Exception {
        String sql = "select * from user where username=? and password=?";
        return queryRunner.query(sql, new BeanHandler<>(User.class), username, password);
    }
}
